<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>给角色设置权限</title>
		<script src="../js/global.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="test1"></div>
		<button class="layui-btn" id="saveModel">保存</button>
		<script>
			layui.use(['tree','laydate'], function() {
				var tree = layui.tree;
				var $ = layui.$;

				$.post(globalData.server + "GetModulesByRoleIdLayUI", {
					rid: 1,
					token: globalData.myToken
				}, function(res) {
					var inst1 = tree.render({
						elem: '#test1', //绑定元素
						data: res,
						showCheckbox: true,
						id: 'demoId'
					});
				}, 'json');

				$('#saveModel').click(function() {
					var checkData = tree.getChecked('demoId');
					//根据递归，获取选中的ids
					var ids = getModelIds(checkData);
					//去除最后一个,
					ids = ids.slice(0, ids.length - 1);
					$.post(globalData.server + "SetSysRights", {
						ids: ids,
						rid: 1,
						token: globalData.myToken
					}, function(res) {
						alert(res.success);
					}, 'json');
				});
				var str = '';

				function getModelIds(arr) {
					//console.log((arr[0].children)[1].children);
					for(var i = 0; i < arr.length; i++) {
						str += arr[i].id + ',';
						if(arr[i].children != undefined) {
							getModelIds(arr[i].children);
						}
					}
					return str;
				}

			});
		</script>
	</body>

</html>